//
//  YSAlert.swift
//  SmartCar
//
//  Created by y s on 2022/10/19.
//

import UIKit

@objcMembers open class YSAlert: YSAlertBase
{
    lazy var buttonHLine:UIView = {
        let result = UIView()
        result.backgroundColor = buttonLineColor
        return result
    }()
    lazy var titleLabel:UILabel = {
        let result = UILabel()
        result.text = title
        result.font = SFitUtil.fitBoldFont(titleFontSize)
        result.textAlignment = titleAlign
        result.textColor = titleColor
        result.numberOfLines = 0
        return result
    }()
    lazy var titleHLine:UIView = {
        let result = UIView()
        result.backgroundColor = buttonLineColor
        return result
    }()
    lazy var contentLabel:UILabel = {
        let result = UILabel()
        result.text = content
        result.font = contentFont
        result.textAlignment = contentAlign
        result.textColor = contentColor
        result.lineBreakMode = .byTruncatingTail
        result.numberOfLines = 0
        return result
    }()
    
    lazy var halfVLine:UIView = {
        let result = UIView()
        result.backgroundColor = buttonLineColor
        return result
    }()
    lazy var sureButton:UIButton = {
        let result = UIButton(type: .custom)
        result.titleLabel?.font = sureBtnFont
        result.setTitleColor(sureColor,for:.normal)
        result.setTitle(sureTitle,for:.normal)
        result.s_clickBlock({[weak self] (tap) in
            if self?.sureBlock != nil{
                self?.sureBlock!()
            }
            self?.hide()
        })
        return result
    }()
    lazy var cancelButton:UIButton = {
        let result = UIButton(type: .custom)
        result.titleLabel?.font = cancelBtnFont
        result.setTitleColor(cancelColor,for:.normal)
        result.setTitle(cancelTitle, for: .normal)
        result.s_clickBlock({[weak self] (tap) in
            self?.hide()
            if self?.cancelBlock != nil{
                self?.cancelBlock!()
            }
        })
        return result
    }()
    public var title:String = ""
    public var content:String = ""
    public var alertScale:CGFloat = 0.8
    public var textHGap:CGFloat = 15
    public var titleFontSize:CGFloat = 16
    public var titleColor:UIColor = STheme.D1
    public var titleAlign:NSTextAlignment = .center
    public var titleVGap:CGFloat = 20
    public var hasTitleHLine = false
    public var contentFont:UIFont = SFitUtil.fitFont(15)
    public var contentColor = STheme.D3
    public var contentLineSpace:CGFloat = 5
    public var contentVGap:CGFloat = 20
    public var contentAttText:NSAttributedString = NSAttributedString()
    public var contentAlign:NSTextAlignment = .left
    public var cancelColor = STheme.D4
    public var sureColor = STheme.D13
    public var lineHGap = 0
    public var lineVGap = 5
    public var buttonHeight:CGFloat = 50
    public var cancelTitle = ""
    public var sureTitle = YMBridging.kLanguage("sure_txt_key")
    public var borderColor = SUIUtil.colorOf(0xd8d8d8)
    public var ncornerRadius:CGFloat = 8
    public var buttonLineColor = SUIUtil.colorOf(0xf2f2f2)
    public var sureBtnFont = SFitUtil.fitBoldFont(14)
    public var cancelBtnFont = SFitUtil.fitFont(14)
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame:CGRect)
    {
        super.init(frame: frame)
        showType = .slideInToCenter
        hideType = .slideOutToCenter
    }
    
    override open func setupUI()
    {
        super.setupUI()
        let window = SJumpUtil.topWindow()
        window.addSubview(self)
        self.frame = CGRect(x:0,y:0,width:ScreenWidth,height:ScreenHeight)
        alertView.backgroundColor = alertViewBackColor
        alertView.layer.borderColor = borderColor.cgColor
        alertView.layer.borderWidth = 0.5
        alertView.layer.cornerRadius = ncornerRadius
        alertView.layer.masksToBounds = true
        alertView.addSubview(buttonHLine)
        addSubview(backgroundView)
        addSubview(alertView)
        if title.count>0{
            alertView.addSubview(titleLabel)
        }
        if title.count>0 && hasTitleHLine{
            alertView.addSubview(titleHLine)
        }
        if content.count>0{
            alertView.addSubview(contentLabel)
            let paragraph = NSMutableParagraphStyle()
            paragraph.lineBreakMode = .byTruncatingTail
            paragraph.lineSpacing = contentLineSpace
            let attributed = NSAttributedString(string: content, attributes:[NSAttributedString.Key.paragraphStyle: paragraph, NSAttributedString.Key.font: contentFont])
            contentLabel.attributedText = attributed
        }
        if contentAttText.length>0{
            alertView.addSubview(contentLabel)
            let mutaleAttStr: NSMutableAttributedString = NSMutableAttributedString(attributedString: contentAttText)
            contentAttText.enumerateAttributes(in: NSMakeRange(0, contentAttText.length), options: .reverse) { (attrs, range, stop) in
                if attrs[NSAttributedString.Key.font] == nil {
                    mutaleAttStr.addAttribute(.font, value: contentFont, range: range)
                }
                if attrs[NSAttributedString.Key.paragraphStyle] == nil {
                    let paragraph = NSMutableParagraphStyle()
                    paragraph.lineBreakMode = .byTruncatingTail
                    paragraph.lineSpacing = SFitUtil.fit(contentLineSpace)
                    mutaleAttStr.addAttribute(.paragraphStyle, value: paragraph, range: range)
                }
            }
            contentLabel.attributedText = contentAttText
        }
        if cancelTitle.count>0{
            alertView.addSubview(cancelButton)
            alertView.addSubview(halfVLine)
        }
        alertView.addSubview(sureButton)
        layout()
        contentLabel.s_clickBlock { (tag) in
            print("")
        }
    }
    
    dynamic func layout()
    {
        let alertW = ScreenWidth*alertScale
        let buttonH = SFitUtil.fit(buttonHeight)
        let titleVG = SFitUtil.fit(titleVGap)
        let contentVG = SFitUtil.fit(contentVGap)
        var alertH:CGFloat = 0
        let titleW:CGFloat = alertW-textHGap*2
        var titleH:CGFloat = 0
        
        contentLabel.textAlignment = contentAlign
        if title.count>0{
            let titleSize = SFitUtil.sizeOf(title, width:titleW,fontSize:titleFontSize)
            titleH = titleSize.height
            alertH += titleVG + titleSize.height
        }
        let contentW:CGFloat = alertW-textHGap*2
        var contentH:CGFloat = 0
        if content.count<1 && contentAttText.length<1{
            hasTitleHLine = false
            alertH += titleVGap
        }else{
            contentH = SFitUtil.heightOf(contentLabel, width:contentW)
            if hasTitleHLine{
                alertH += titleVGap + contentVG
            }else if title.count<1{
                alertH += titleVGap
            }else{
                alertH += contentVGap
            }
            alertH += contentH + contentVG
        }
        alertH += buttonH
        alertView.frame = CGRect(x:(ScreenWidth-alertW)*0.5,y:(ScreenHeight-alertH)*0.5,width:alertW,height:alertH)
        if title.count>0{
            titleLabel.snp.makeConstraints { (make) in
                make.top.equalTo(titleVG)
                make.left.equalTo(textHGap)
                make.right.equalTo(-textHGap)
                make.height.equalTo(titleH)
            }
            if hasTitleHLine{
                titleHLine.snp.makeConstraints { (make) in
                    make.left.equalTo(lineHGap)
                    make.right.equalTo(-lineHGap)
                    make.top.equalTo(titleLabel.snp.bottom).offset(titleVG)
                    make.height.equalTo(LineHeight)
                }
            }
        }
        if contentLabel.superview != nil{
            contentLabel.snp.remakeConstraints { (make) in
                if hasTitleHLine{
                    make.top.equalTo(titleHLine.snp.bottom).offset(contentVG)
                }else if title.count>0{
                    make.top.equalTo(titleLabel.snp.bottom).offset(contentVG)
                }else{
                    make.top.equalTo(titleVG)
                }
                make.centerX.equalToSuperview()
                make.width.equalTo(contentW)
                make.height.equalTo(contentH)
            }
        }
        buttonHLine.snp.makeConstraints { (make) in
            make.left.right.equalTo(0)
            make.bottom.equalTo(-buttonH)
            make.height.equalTo(0.5)
        }
        if cancelTitle.count>0{
            cancelButton.snp.makeConstraints { (make) in
                make.left.bottom.equalTo(0)
                make.top.equalTo(buttonHLine.snp.bottom)
                make.width.equalTo(alertW*0.5)
            }
            halfVLine.snp.makeConstraints { (make) in
                make.centerX.equalToSuperview()
                make.width.equalTo(0.5)
                make.top.equalTo(buttonHLine.snp.bottom)
                make.bottom.equalTo(0)
            }
        }
        sureButton.snp.makeConstraints { (make) in
            if cancelTitle.count<1{
                make.left.equalTo(0)
            }else{
                make.left.equalTo(alertW*0.5)
            }
            make.right.bottom.equalTo(0)
            make.top.equalTo(buttonHLine.snp.bottom)
        }
    }
}
